package com.ciba.demo.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciba.demo.mapper.UserInfoMapper;
import com.ciba.demo.mapper.UserPlanMapper;
import com.ciba.demo.mapper.WordSelectedMapper;
import com.ciba.demo.pojo.UserInfo;
import com.ciba.demo.pojo.UserPlan;
import com.ciba.demo.pojo.WordSelected;
import com.ciba.demo.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

@Component
@Configurable
@EnableScheduling
public class ToStaticUtils {

    @Autowired
    WordSelectedMapper wordSelectedMapper ;

    @Autowired
    UserInfoMapper userInfoMapper ;

    @Autowired
    PlanService planService ;

    @Autowired
    UserPlanMapper userPlanMapper ;

    @Resource
    RedisTemplate redisTemplate ;
    private static String Record = "Record" ;

    @Scheduled(cron="*/30 * * * * * ")
    public void reportCurrentByCron(){
        int count = 0 ;
        Set<String>keys = redisTemplate.keys("*") ;
        for(String key : keys){
            if(key.matches(Record+"_.*")){
                //持久化
                int max = -1 ;
                UserInfo userInfo = null ;
                UserPlan userPlan = null ;
                BoundListOperations boundListOperations = redisTemplate.boundListOps(key);
                for(int i=0;i<boundListOperations.size();i++){
                    WordSelected wordSelected = (WordSelected) boundListOperations.index(i) ;
                    QueryWrapper<WordSelected> wrapper = new QueryWrapper<>() ;
                    if(i==0){
                        userInfo = userInfoMapper.selectById(wordSelected.getUserId()) ;
                        userPlan =  planService.getPlan(userInfo) ;
                        max = userPlan.getNextWord() ;
                    }
                    Map<String,Object> map  = new HashMap<>() ;
                    map.put("word_id",wordSelected.getWordId());
                    map.put("user_id",wordSelected.getUserId());
                    wrapper.allEq(map) ;
                    if(wordSelectedMapper.update(wordSelected,wrapper)>01){
                        wordSelectedMapper.insert(wordSelected) ;
                    }
                    max = wordSelected.getWordId() > max ? wordSelected.getWordId() : max ;
                }
                userPlan.setNextWord(max);
                userPlanMapper.updateById(userPlan) ;
                count++ ;
                redisTemplate.delete(key);
            }
        }
        System.out.println("持久化成功！"+count+"条记录被持久化！");
    }
}

